#!/bin/bash
# Leebor
# 2017-11-1
# (stop|start|restart) php-fpm
# v1.0

Install="/opt/php7"
Ini=$Install/etc/php.ini
Fpm=$Install/etc/php-fpm.conf
Pid=$Install/run/php-fpm.pid

. /etc/rc.d/init.d/functions

case $1 in
    start)
        if [ ! -f $Pid ];then
            if [ -x $Install/sbin/php-fpm ] && [ -f $Ini ] && [ -f $Fpm ];then
		        $Install/sbin/php-fpm -c $Ini -y $Fpm
                if [ $? = 0 ];then
                    echo ">>>>>> Start Php-fpm Success!"
					
                else
                    echo ">>>>>> Start Php-fpm Error!"
	        fi
	    else
                echo ">>>>>> File error!"			
            fi
	else
            echo ">>>>>> Pid exists, Need Stop!"
        fi
    ;;	
    stop)
	if [ $(pgrep php-fpm | wc -l) -eq 0 ];then
            echo ">>>>>> Pid not exists!"
        else
            if [ -f $Pid ];then
                kill -INT $(cat $Pid) >> /dev/null 2>&1
            else
                echo ">>>>>> Pid file not exists"
                exit 1   
            fi
	    if [ $? -eq 0 ];then
                echo ">>>>>> Stop Success!"
            else
		echo ">>>>>> Stop Error!"
	    fi
        fi
    ;;
    restart)
        stop
	if [ $? -eq 0 ];then
            Start
        else
            exit
        fi
        ;;
    *)
        echo "Usage : php-fpm (start|stop|restart)"
    ;;	    
esac